<?php
namespace app\common\order\model;

use app\common\config\logic\CommonPickerLogic;
use app\common\config\logic\DictionaryLogic;
use app\common\config\model\ConfigCompany;
use app\common\config\model\ConfigHall;
use app\common\config\model\ConfigStore;
use app\common\customer\model\Customer;
use app\common\customer\model\CustomerAllocate;
use app\common\product\logic\StoreCategoryLogic;
use app\common\system\logic\UserLogic;
use Symfony\Component\VarDumper\Cloner\Data;
use think\db\exception\DataNotFoundException;
use think\Model;
use think\model\concern\SoftDelete;

class Order extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;
    

    protected $type = [
        'files' => 'json',
        'sign_date' => 'timestamp',
        'event_date' => 'timestamp',
        'check_time' => 'timestamp',
    ];

    public function member()
    {
        return $this->hasOne(Customer::class, "id", "member_id");
    }

    public function product()
    {
        return $this->hasOne(OrderProduct::class, "order_id", "id");
    }

    public function getCustomerAttr($value, $data) 
    {
        $customerModel = new Customer();
        $fields = "id,fullname,create_id,source_id";
        try {
            $customer = $customerModel->where('id', $data['member_id'])->field($fields)->find();
            $customer->append(['creator', 'source']);
        } catch (DataNotFoundException $e) {
            $customer = [
                'id' => '',
                'fullname' => '',
                'creator' => [
                    'id' => '',
                    'realname' => '',
                ],
                'source' => [
                    'id' => '',
                    'title' => ''
                ]
            ];
        }

        return $customer;
    }

    public function getAllocateAttr($value, $data) {
        if ($data['allocate_id'] > 0) {
            $allocateModel = new CustomerAllocate();
            $allocate = $allocateModel->where('id', $data['allocate_id'])->field('id,user_id')->find();
            $allocate->append(['user']);
        } else {
            $allocate = [
                'id' => '',
                'user_id' => '',
                'user' => [
                    'id' => '',
                    'realname' => ''
                ]
            ];
        }

        return $allocate;
    }

    public function getCategoryAttr($value, $data) {
        $categoryLogic = new StoreCategoryLogic();
        return $categoryLogic->getStoreCategory($data['category_id'], ['id', 'title', 'order_model']);
    }

    public function getCompanyAttr($value, $data) {
        $dictionaryLogic = new DictionaryLogic();
        return $dictionaryLogic->getItem($data['company_id'], ConfigCompany::class, ['id', 'title', 'short_name']);
    }

    public function getCooperationAttr($value, $data) {
        $commonLogic = new CommonPickerLogic();
        return $commonLogic->getPickerItem('cooperation', $data['cooperation_id']);
    }

    public function getStoreAttr($value, $data) {
        $dictionaryLogic = new DictionaryLogic();
        return $dictionaryLogic->getItem($data['store_id'], ConfigStore::class, ['id', 'title']);
    }

    public function getHallAttr($value, $data) {
        $dictionaryLogic = new DictionaryLogic();
        return $dictionaryLogic->getItem($data['hall_id'], ConfigHall::class, ['id', 'title']);
    }

    public function getBanquetTypeAttr($value, $data) {
        $commonLogic = new CommonPickerLogic();
        return $commonLogic->getPickerItem('banquetType', $data['banquet_type_id']);
    }

    public function getSignDateAttr($value) {
        return date('Y-m-d', $value);
    }

    public function getEventDateAttr($value) {
        return date('Y-m-d', $value);
    }

    public function getUserAttr($value, $data) {
        $userLogic = new UserLogic();
        return $userLogic->getUser($data['user_id'], ['id', 'realname', 'work_wechat']);
    }

    public function getOrderTypeTextAttr($value, $data) {
        $index = $data['order_type'];
        $map = [
            1 => '订单',
            2 => '返佣单',
            3 => '定金订单',
            4 => '意向金订单'
        ];

        return $map[$index];
    }

    public function getCheckTagAttr($value, $data) {
        $index = $data['check_status'];
        $map = [
            -1 => '暂存',
            0 => '待审核',
            1 => '审核中',
            2 => '审核通过',
            3 => '审核驳回'
        ];

        return $map[$index];
    }

    public function getCollectsAttr($value, $data) {
        $model = new OrderCollect();
        $rows = $model->where('order_id', $data['id'])->select();
        foreach ($rows as $row) {
            $row->append(['title', 'check_tag']);
        }

        return $rows;
    }
}